package com.sw.small_world.fragment.me.mycircle;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.TextView;

import com.sw.small_world.R;
import com.sw.small_world.activity.ClickButtonActivity;
import com.sw.small_world.bean.BooleanBean;
import com.sw.small_world.bean.UserBean;
import com.sw.small_world.fragment.base.BaseFragment;
import com.sw.small_world.utils.ConstantUtils;
import com.sw.small_world.utils.LogUtils;
import com.sw.small_world.utils.StartUtils;
import com.sw.small_world.widgets.CircleImageView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/3/16.
 */

public class AddCircleFragment extends BaseFragment {

    @Bind(R.id.gridView)
    GridView gridView;
    @Bind(R.id.search)
    EditText search;
    @Bind(R.id.list_view)
    ExpandableListView listView;
    private ClickButtonActivity activity;
    ArrayList<UserBean> gridUser = new ArrayList<>();
    ArrayList<ArrayList<UserBean>> children = new ArrayList<>();
    ArrayList<ArrayList<BooleanBean>> listChecked = new ArrayList<>();
    private GridAdapter gridAdapter;
    private ListAdapter listAdapter;
    ArrayList<String> groups = new ArrayList<>();

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (ClickButtonActivity) getActivity();
        setActionBar();
    }

    @Override
    protected Object requestData() {
        for (int i = 0; i < 10; i++) {
            groups.add("");
            ArrayList<UserBean> userList = new ArrayList<>();
            ArrayList<BooleanBean> booleanList = new ArrayList<>();
            for(int j = 0;j<3;j++){
                userList.add(new UserBean());
                booleanList.add(new BooleanBean(false));
            }
            children.add(userList);
            listChecked.add(booleanList);
        }
        return ConstantUtils.STATE_SUCCESSED;
    }

    @Override
    protected View getSuccessView() {
        View header = View.inflate(activity, R.layout.header_add_circle, null);
        ButterKnife.bind(this, header);
        listAdapter = new ListAdapter();
        gridAdapter = new GridAdapter();
        listView.setAdapter(listAdapter);
        gridView.setAdapter(gridAdapter);
        listView.setGroupIndicator(null);// 去掉默认箭头
        for (int i = 0; i < groups.size(); i++) {// 默认全部展开
            listView.expandGroup(i);
        }
        listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {// 点击不能收缩
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true;
            }
        });

        return header;
    }

    @Override
    public void onClick(View v) {

    }

    private void setActionBar() {
        activity.tvTitle.setText("我的人脉");
        activity.tvRight.setVisibility(View.VISIBLE);
        activity.tvRight.setText("完成");
        activity.tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StartUtils.startActivityById(activity, ConstantUtils.CREATE_TAG);
                activity.finish();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    class GridAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return gridUser.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(activity, R.layout.item_header_grid, new FrameLayout(activity));
            }
            return convertView;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }
    }
    class ListAdapter extends BaseExpandableListAdapter{


        @Override
        public int getGroupCount() {
            return groups.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return children.get(groupPosition).size();
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            GViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(activity, R.layout.item_my_friends_group, null);
                holder = new GViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (GViewHolder) convertView.getTag();
            }
            return convertView;
        }

        @Override
        public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
           ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(activity, R.layout.item_add_circle, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            LogUtils.e("zs",groupPosition+"=="+childPosition);
            holder.cbAdd.setChecked(listChecked.get(groupPosition).get(childPosition).isChecked());
            holder.cbAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listChecked.get(groupPosition).get(childPosition).setChecked(!listChecked.get(groupPosition).get(childPosition).isChecked());
                    if (listChecked.get(groupPosition).get(childPosition).isChecked()) {
                        gridUser.add(children.get(groupPosition).get(childPosition));
                    } else {
                        gridUser.remove(children.get(groupPosition).get(childPosition));
                    }
                    gridAdapter.notifyDataSetChanged();
                }
            });
            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        @Override
        public boolean areAllItemsEnabled() {
            return false;
        }

        @Override
        public boolean isEmpty() {
            return false;
        }

        @Override
        public void onGroupExpanded(int groupPosition) {

        }

        @Override
        public void onGroupCollapsed(int groupPosition) {

        }

        @Override
        public long getCombinedChildId(long groupId, long childId) {
            return 0;
        }

        @Override
        public long getCombinedGroupId(long groupId) {
            return 0;
        }

        @Override
        public void registerDataSetObserver(DataSetObserver observer) {

        }

        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {

        }

        @Override
        public Object getGroup(int groupPosition) {
            return null;
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return null;
        }

        @Override
        public long getGroupId(int groupPosition) {
            return 0;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return 0;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        class GViewHolder {

            @Bind(R.id.tv_group)
            TextView tvGroup;

            GViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }

        class ViewHolder {
            @Bind(R.id.cb_add)
            CheckBox cbAdd;
            @Bind(R.id.iv_portrait)
            CircleImageView ivPortrait;
            @Bind(R.id.tv_name)
            TextView tvName;
            @Bind(R.id.tv_deep)
            TextView tvDeep;
            @Bind(R.id.tv_company_position)
            TextView tvCompanyPosition;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }

}
